<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org">
    <head>
        <title>Spring Security OAuth 2.0 Sample</title>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <link rel="stylesheet" href="/webjars/bootstrap/css/bootstrap.css" th:href="@{/webjars/bootstrap/css/bootstrap.css}" />
	</head>
    <body>
        <div class="container">
            <h1>Oauth密码模式登录</h1>
            <p th:if="${loginError}" style="font-weight:bold;color:red;">Wrong username or password</p>
            <div class="form-row">
                <div class="form-group">
                    <label for="username">Username</label>
                    <input type="text" id="username" name="username" autofocus="autofocus" class="form-control">
                    <small class="form-text text-muted">user1 / password</small>
                </div>
            </div>
            <div class="form-row">
                <div class="form-group">
                    <label for="password">Password</label>
                    <input type="password" id="password" name="password" class="form-control">
                </div>
            </div>
            <button id="login" class="btn btn-primary">Log in</button>
        </div>
        <script src="//cdn.bootcss.com/jquery/1.12.4/jquery.js"></script>
        <script src="//cdn.bootcss.com/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
        <script src="/webjars/bootstrap/js/bootstrap.min.js" th:src="@{/webjars/bootstrap/js/bootstrap.min.js}"></script>
        <script>
            $("#login").click(function () {
                var data = {
                    "username": $("#username").val(),
                    "password": $("#password").val()
                }
                $.ajax({
                    //请求方式
                    type : "POST",
                    //请求的媒体类型
                    contentType: "application/json;charset=UTF-8",
                    //请求地址
                    url : "http://127.0.0.1:6090/doLogin",
                    dataType:'json',
                    //数据，json字符串
                    data : JSON.stringify(data),
                    //请求成功
                    success : function(result) {
                        var token = result['access_token'];
                        if (token == '') {
                            alert("登录失败")
                        }
                        $.cookie('access_token', token);
                        window.location.replace("http://localhost:6090/home");
                    },
                    //请求失败，包含具体的错误信息
                    error : function(e){
                        console.log(e.status);
                        console.log(e.responseText);
                        alert("登录失败"+e.responseText);
                    }
                });
            })
        </script>
    </body>
</html>